import 'package:flutter/material.dart';
import 'dart:ui';

class Adapt{
  static Adapt instance = Adapt();
  static MediaQueryData mediaQuery = MediaQueryData.fromWindow(window);
  static double _width = mediaQuery.size.width;
  static double _height = mediaQuery.size.height;
  static double _topBarH = mediaQuery.padding.top;
  static double _bottomBarH = mediaQuery.padding.bottom;
  static double _pixelRatio = mediaQuery.devicePixelRatio;
  static double _textScaleFactor = mediaQuery.textScaleFactor;

  static bool _allowFontScaling = false;
  static double _uiWidth = 375.0;
  static double _uiHeight = 812.0;

  void init(BuildContext context){
    mediaQuery = MediaQuery.of(context);
    _width = mediaQuery.size.width;
    _height = mediaQuery.size.height;
    _topBarH = mediaQuery.padding.top;
    _bottomBarH = mediaQuery.padding.bottom;
    _pixelRatio = mediaQuery.devicePixelRatio;
    _textScaleFactor = mediaQuery.textScaleFactor;
  }

  static Adapt getInstance(){
    return instance;
  }
  /// 实际的dp与UI设计px的比例
  static get scaleWidth => _width/_uiWidth;
  static get scaleHeight => _height/_uiHeight;

  /// 当前设备宽度 px
  static double get screenW => _width * _pixelRatio;
  /// 当前设备高度 px
  static double get screenH => _height * _pixelRatio;

  // 设置字体大小px
  static px(number){
    return _allowFontScaling ? (number * scaleWidth) / _textScaleFactor : number*scaleWidth;
  }

  static onepx(){
    return 1 / _pixelRatio;
  }

  static padTopH(){
    return _topBarH;
  }

  //?
  double setPx(number){
    return _allowFontScaling ? (number * scaleWidth) / _textScaleFactor : number*scaleWidth;
  }

  static padBottomH(){
    return _bottomBarH;
  }

  static width(double width) => width * scaleWidth;

  static height(double height) => height * scaleHeight;
}